package com.orcamento.controller;

import org.springframework.beans.factory.annotation.Autowired;

import com.orcamento.components.CalendarPanel;
import com.orcamento.entity.enums.Month;
import com.orcamento.entity.report.HeritageReport;
import com.orcamento.entity.report.Report;
import com.orcamento.service.ReportService;
import com.orcamento.service.filter.DateFilter;
import com.orcamento.util.CalendarUtil;

@SuppressWarnings("serial")
public class ReportController extends AbstractController {

	@Autowired
	private transient ReportService reportService;
	
	private String heritageReportJson;
	private Report report;
	private CalendarPanel calendarPanel;
	
	private Month month;
	private Integer year;
	
	private DateFilter dateFilter;
	
	public String overview() {
		month = month != null ? month : Month.findById( CalendarUtil.getCurrentMonth() );
		year  = year != null ? year : CalendarUtil.getCurrentYear(); 
		
		calendarPanel = new CalendarPanel(year, month, "report", "overview");
		setReport( reportService.overview(calendarPanel.getYear(), calendarPanel.getMonth(), getAccountId()) );
		return SUCCESS;
	}
	
	public String generalBalance() {
		month = Month.findById( CalendarUtil.getCurrentMonth() );
		setYear( CalendarUtil.getCurrentYear() );
		setReport( reportService.generalBalance( year, month, getAccountId() ) );
		return SUCCESS;
	}

	public String categories() {
		month = month != null ? month : null;
		year  = year  != null ? year  : null;
		
		calendarPanel = new CalendarPanel(year, month, "report", "categories");
		
		setReport( reportService.categories(calendarPanel.getYear(), calendarPanel.getMonth(), getAccountId()) );
		return SUCCESS;
	}
	
	public String events() {
		month = month != null ? month : null;
		year  = year  != null ? year  : null;
		
		calendarPanel = new CalendarPanel(year, month, "report", "categories");
		
		setReport( reportService.events(calendarPanel.getYear(), calendarPanel.getMonth(), getAccountId()) );
		return SUCCESS;
	}
	
	public String heritage() {
		month = month != null ? month : Month.findById( CalendarUtil.getCurrentMonth() );
		year  = year != null ? year : CalendarUtil.getCurrentYear(); 

		report = (HeritageReport) reportService.heritage(year, month, getAccountId());
		return SUCCESS;
	}
	
	public Report getReport() {
		return report;
	}

	public void setReport(Report report) {
		this.report = report;
	}

	public void setMonth(String month) {
		this.month = Month.findById( Integer.valueOf(month) );
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public CalendarPanel getCalendarPanel() {
		return calendarPanel;
	}

	public DateFilter getDateFilter() {
		return dateFilter;
	}

	public void setDateFilter(DateFilter dateFilter) {
		this.dateFilter = dateFilter;
	}
	
	public Integer getMonth() {
		try {
			return month.id;
		} catch(Exception e) {
			return null;
		}
	}
	
	public Integer getYear() {
		return year;
	}

	public String getHeritageReportJson() {
		return heritageReportJson;
	}

	public void setHeritageReportJson(String heritageReportJson) {
		this.heritageReportJson = heritageReportJson;
	}
	
}
